package com.selfdriving.activity;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.selfdriving.R;
import com.selfdriving.utils.Constant;
import com.selfdriving.utils.Utils;
import com.selfdriving.utils.ViewFlipperView;

public class FeatureDetailsActivity extends BaseActivity
{
	@ViewInject(id = R.id.head_break, click = "topBack")
	ImageView topBack;

	@ViewInject(id = R.id.top_text_middle)
	TextView topText;

	@ViewInject(id = R.id.vf_layout)
	LinearLayout viewFlipperRL;

	@ViewInject(id = R.id.radio_layout)
	LinearLayout Lradio;

	@ViewInject(id = R.id.rlFeature, click = "rlFeature")
	RelativeLayout rlFeature;

	@ViewInject(id = R.id.rlHotel, click = "rlHotel")
	RelativeLayout rlHotel;

	@ViewInject(id = R.id.rlRestaurant, click = "rlRestaurant")
	RelativeLayout rlRestaurant;

	@ViewInject(id = R.id.rlMall, click = "rlMall")
	RelativeLayout rlMall;

	@ViewInject(id = R.id.goOver, click = "goOver")
	TextView goOver;

	@ViewInject(id = R.id.wantGo, click = "wantGo")
	TextView wantGo;

	@ViewInject(id = R.id.tvNum)
	TextView tvNum;

	@ViewInject(id = R.id.tvNum1)
	TextView tvNum1;

	@ViewInject(id = R.id.tvNum2)
	TextView tvNum2;
	@ViewInject(id = R.id.tvNum3)
	TextView tvNum3;

	@ViewInject(id = R.id.viewflipper_size)
	TextView round;

	String sid;
	String name;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feature_details_test);
		topBack.setVisibility(View.VISIBLE);
		Intent intent = super.getIntent();
		sid = intent.getStringExtra("sid");
		name = intent.getStringExtra("name");
		topText.setText(name + "攻略");
		post(sid);
	}

	public void topBack(View v)
	{
		this.finish();
	}

	public void goOver(View v)
	{
		gotoScenic("26");
	}

	public void wantGo(View v)
	{
		gotoScenic("27");
	}

	/** 宾馆 */
	public void rlHotel(View v)
	{
		Intent intent = new Intent(this, HotelListActivity.class);
		intent.putExtra("sid", sid);
		intent.putExtra("name", name);
		startActivity(intent);
	}

	/** 餐厅 */
	public void rlRestaurant(View v)
	{
		Intent intent = new Intent(this, RestaurantListActivity.class);
		intent.putExtra("sid", sid);
		intent.putExtra("name", name);
		startActivity(intent);
	}

	/** 购物 */
	public void rlMall(View v)
	{
		Intent intent = new Intent(this, MallListActivity.class);
		intent.putExtra("sid", sid);
		intent.putExtra("name", name);
		startActivity(intent);
	}

	/** 景点 */
	public void rlFeature(View v)
	{
		Intent intent = new Intent(this, FeatureListActivity.class);
		intent.putExtra("sid", sid);
		intent.putExtra("name", name);
		startActivity(intent);
	}

	public void gotoScenic(final String host)
	{
		showLoadingDialog("请求发送中...");
		AjaxParams params = new AjaxParams();
		params.put("f", host);
		params.put("sid", sid);
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				dismissLoadingDialog();
			}

			@Override
			public void onSuccess(Object o)
			{
				if (o.toString().contains("000000"))
				{
					if (host.equals("26"))
					{
						int x = Integer.parseInt(goOver.getText().toString());
						goOver.setText("" + (x + 1));
					} else if (host.equals("27"))
					{
						int x = Integer.parseInt(wantGo.getText().toString());
						wantGo.setText("" + (x + 1));
					}
				}
				dismissLoadingDialog();
			}
		});
	}

	public void post(String sid)
	{
		if (viewFlipperRL.getChildCount() > 0)
		{
			viewFlipperRL.removeAllViews();
		}
		showLoadingDialog("数据加载中...");
		AjaxParams params = new AjaxParams();
		params.put("f", "25");
		params.put("sid", sid);
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				dismissLoadingDialog();
				showToast("数据加载失败");
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				try
				{
					JSONObject object = new JSONObject(json);
					String img0 = null;
					String img1 = null;
					String img2 = null;
					String img3 = null;
					String img4 = null;
					String imgs[] = null;
					if (object.has("img0"))
					{
						imgs = new String[1];
						img0 = object.getString("img0");
						imgs[0] = img0;
					}
					if (object.has("img1"))
					{
						imgs = new String[2];
						img1 = object.getString("img1");
						imgs[0] = img0;
						imgs[1] = img1;
					}
					if (object.has("img2"))
					{
						imgs = new String[3];
						img2 = object.getString("img2");
						imgs[0] = img0;
						imgs[1] = img1;
						imgs[2] = img2;
					}
					if (object.has("img3"))
					{
						imgs = new String[4];
						img3 = object.getString("img3");
						imgs[0] = img0;
						imgs[1] = img1;
						imgs[2] = img2;
						imgs[3] = img3;
					}
					if (object.has("img4"))
					{
						imgs = new String[5];
						img4 = object.getString("img4");
						imgs[0] = img0;
						imgs[1] = img1;
						imgs[2] = img2;
						imgs[3] = img3;
						imgs[4] = img4;
					}
					String arrive = object.getString("arrive");
					String wantGo = object.getString("wantGo");
					String jds = object.getString("jds");
					String hotels = object.getString("hotels");
					String repast = object.getString("repast");
					String shopping = object.getString("shopping");
					String fallow = object.getString("fallow");
					tvNum.setText(jds);
					tvNum1.setText(hotels);
					tvNum2.setText(repast);
					tvNum3.setText(shopping);
					goOver.setText(arrive);
					FeatureDetailsActivity.this.wantGo.setText(wantGo);
					ViewFlipperView flipperView = new ViewFlipperView(
							mApplication, FeatureDetailsActivity.this, round,
							Lradio, 0, imgs);
					viewFlipperRL
							.addView(flipperView.viewFlipper, layoutParams);
					dismissLoadingDialog();
				} catch (JSONException e)
				{
					Utils.dismissLoadingDialog();
					showToast("数据加载失败");
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	public void onBackPressed()
	{
		finish();
	}
}
